package com.ce.pms.occupation.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.occupation.dao.IPerfectItemRequisitionDao;
import com.ce.pms.occupation.model.PerfectItemRequisitionDO;
import com.ce.pms.occupation.vo.PerfectItemSearchVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: 整改项目登记DAO实现类
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 胡煦 Create on:2008-12-30 Edit author: Edit on: 修改原因：
 *
 */

public class PerfectItemRequisitionDaoImpl extends
		PmsBaseDaoImpl<PerfectItemRequisitionDO> implements
		IPerfectItemRequisitionDao {
	/**
	 * 方法描述: 分页查询所有整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param SearchVO
	 * @return 返回当前页的查询结果 Edit author: Edit on: 修改原因：
	 */
	public Page queryPerfectItemRequisitionDOByPage(PerfectItemSearchVO searchVO) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		StringBuilder queryStr = null;
		if (!BlankUtil.isBlank(searchVO.getCompleteDate1())||!BlankUtil.isBlank(searchVO.getCompleteDate2())) {
			queryStr  = new StringBuilder(
			"select pr from PerfectItemTrackDO pt left join pt.perfectItemRequisition pr where 1 = 1 ");
		}else{
			queryStr = new StringBuilder(
			"from PerfectItemRequisitionDO pr where 1 =1 ");
		}
		queryStr.append(this.generateWhereAndOrderSentence(searchVO,
				parameterMap));
		Page page = super.pagedNameQueryByPageNo(queryStr.toString(), searchVO
				.getPageNo(), searchVO.getPageSize(), parameterMap);
		return page;
	}
	/**
	 * 方法描述: 根据派工ID查询所有整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param perfectItemDispatchId,searchVO,isCompleted(为true时查询已经完工的，否则相反)
	 * @return List<PerfectItemRequisitionDO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemRequisitionDO> queryPerItemListByDispatchId(PerfectItemSearchVO searchVO) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		StringBuilder queryStr = new StringBuilder("from PerfectItemRequisitionDO pr where 1 = 1 ");
		queryStr.append(this.generateWhereAndOrderSentence(searchVO,
				parameterMap));
		Query query = super.createNameQuery(queryStr.toString(), parameterMap);
		return query.list();
	}
	/**
	 * 方法描述: 根据验房ID查询所有整改项目登记
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param inspecteRequisiteId
	 * @return List<PerfectItemRequisitionDO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemRequisitionDO> queryPerItemListByInspReqId(Long inspecteRequisiteId) {
		String queryStr ="from PerfectItemRequisitionDO pr where pr.inspectionRequisition.keyword = ? ";
		Query query = super.createQuery(queryStr, inspecteRequisiteId);
		return query.list();
	}
	/**
	 * 功能描述:生成查询所有整改项目列表的条件和排序子句 <br>
	 *
	 * @param searchVO,Map
	 * @return String Create author:胡煦 <br>
	 *         Create on:2008-12-29 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private String generateWhereAndOrderSentence(PerfectItemSearchVO searchVO,
			Map<String, Object> parameterMap) {
		StringBuilder sb = new StringBuilder();
		// 楼栋ID
		if (!BlankUtil.isBlank(searchVO.getBuildingId())) {
			sb
					.append(" and pr.inspectionRequisition.identityValidate.house.building.keyword = :building");
			parameterMap.put("building", searchVO.getBuildingId());
		}
		// 业主姓名
		if (!BlankUtil.isBlank(searchVO.getOwnerName())) {
			sb.append(" and pr.owners = :ownerName");
			parameterMap.put("ownerName", searchVO.getOwnerName());
		}
		// 派工ID
		if (!BlankUtil.isBlank(searchVO.getPerfectItemDispatchId())) {
			sb
					.append(" and pr.perfectItemDispatch.keyword = :perfectItemDispatchId");
			parameterMap.put("perfectItemDispatchId", searchVO
					.getPerfectItemDispatchId());
		}
		// 申报项目类型ID
		if (!BlankUtil.isBlank(searchVO.getPerfectItemTypeConfigId())) {
			sb
					.append(" and pr.perfectItemTypeConfig.keyword = :perfectItemTypeConfigId");
			parameterMap.put("perfectItemTypeConfigId", searchVO
					.getPerfectItemTypeConfigId());
		}
		// 房号
		if (!BlankUtil.isBlank(searchVO.getHouseNumber())) {
			sb
					.append(" and pr.inspectionRequisition.identityValidate.house.houseNumber like :houseNumber");
			parameterMap.put("houseNumber", "%" + searchVO.getHouseNumber()
					+ "%");
		}
		// 房间ID
		if (!BlankUtil.isBlank(searchVO.getHouseId())) {
			sb
					.append(" and pr.inspectionRequisition.identityValidate.house.keyword = :houseId");
			parameterMap.put("houseId", searchVO.getHouseId());
		}
		// 问题描述
		if (!BlankUtil.isBlank(searchVO.getDescription())) {
			sb.append(" and pr.description like :description");
			parameterMap.put("description", "%" + searchVO.getDescription()
					+ "%");
		}
		// 整改申请日期1
		if (!BlankUtil.isBlank(searchVO.getPerfectRequisitionDate1())) {
			sb
					.append(" and pr.perfectRequisitionDate >= :perfectRequisitionDate1");
			parameterMap.put("perfectRequisitionDate1", searchVO
					.getPerfectRequisitionDate1());
		}
		// 整改申请日期2
		if (!BlankUtil.isBlank(searchVO.getPerfectRequisitionDate2())) {
			sb
					.append(" and pr.perfectRequisitionDate <= :perfectRequisitionDate2");
			parameterMap.put("perfectRequisitionDate2", searchVO
					.getPerfectRequisitionDate2());
		}
		// 完工日期1
		if (!BlankUtil.isBlank(searchVO.getCompleteDate1())) {
			sb.append(" and pt.completeDate >= :completeDate1");
			parameterMap.put("completeDate1", searchVO.getCompleteDate1());
		}
		// 完工日期2
		if (!BlankUtil.isBlank(searchVO.getCompleteDate2())) {
			sb.append(" and pt.completeDate <= :completeDate2");
			parameterMap.put("completeDate2", searchVO.getCompleteDate2());
		}
		//完工状态
		if (!BlankUtil.isBlank(searchVO.getCompleteYn())) {
			sb.append(" and IFNULL(pr.completeYn,0) = :completeYn ");
			parameterMap.put("completeYn", searchVO.getCompleteYn());
		}
		//派工状态
		if (!BlankUtil.isBlank(searchVO.getDispatchYn())) {
			sb.append(" and pr.dispatchYn = :dispatchYn");
			parameterMap.put("dispatchYn", searchVO.getDispatchYn());
		}
		//验收状态
		if (!BlankUtil.isBlank(searchVO.getCheckResult())) {
			sb
					.append(" and pr.inspectionRequisition.checkResult = :checkResult");
			parameterMap.put("checkResult", searchVO.getCheckResult());
		}
		if (!BlankUtil.isBlank(searchVO.getOrderKey())
				&& !BlankUtil.isBlank(searchVO.getOrderValue())) {
			if("buildingName".equals(searchVO.getOrderValue())){
				sb
				.append(" order by pr.inspectionRequisition.identityValidate.house.building.name ").
				append(searchVO.getOrderKey());
			}else{
				sb.append(" order by pr.").append(searchVO.getOrderValue()).append(
				" ").append(searchVO.getOrderKey());
			}
		} else {
			sb
					.append(" order by pr.inspectionRequisition.identityValidate.house.houseNumber");
		}
		return sb.toString();
	}
	/**
	 * 方法描述: 根据整改项目ID查询完工时间方法
	 *
	 * @author 胡煦 Create on:2008-12-30
	 * @param perfectItemRequisitionId
	 * @return Date Edit author: Edit on: 修改原因：
	 */
	public Date getCompleteDateById(Long perfectItemRequisitionId){
		String hql = "select pt.completeDate from PerfectItemTrackDO pt left join pt.perfectItemRequisition pr where pr.id = ? order by pt.id desc";
		Query query = super.createQuery(hql, perfectItemRequisitionId);
		return query.list().size()==0?null:(Date) query.list().get(0);
	}
	/**
	 * 方法描述: 根据整改项目派工ID查询未完工整改项目List
	 *
	 * @author 胡煦 Create on:2008-12-30
	 * @param List<PerfectItemRequisitionDO>
	 * @return Date Edit author: Edit on: 修改原因：
	 */
	public List<PerfectItemRequisitionDO> getPerItemsByDispatchId(Long perfectItemDispatchId){
		if(BlankUtil.isBlank(perfectItemDispatchId)){
			return null;
		}
		String hql = "from PerfectItemRequisitionDO pr where pr.perfectItemDispatch.keyword = ? and pr.dispatchYn = 1 and pr.completeYn = 0 ";
		Query query = super.createQuery(hql, perfectItemDispatchId);
		return query.list();
	}

	/**
	 * 方法描述: 批量修改整改项目
	 * @author 胡煦
	 * Create on:2008-9-9
	 * @param List<Object[]> list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchUpadateDispatchs(List<Object[]> list){
		String sql = "UPDATE OM_PERFECT_ITEM_REQUISITION SET COMPLETE_YN = 1 WHERE PERFECT_ITEM_REQUISITION_ID = ?";
		this.executeSQLBatch(sql, list);
		return true;
	}
}
